package com.card.user;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.xiaoao.net.db.DBManager;
import com.xiaoao.net.db.Entity;
import com.xiaoao.net.utils.Log;
import com.xiaoao.net.utils.PubFunc;
import com.xiaoao.net.utils.StringUtil;
import com.xiaoao.net.utils.json.GSONUtils;

public class EntityBase {
	//数据实体
	transient private	Entity	entity;
	transient
	Gson		Gson;
	
	transient boolean		userEntityModified;
	
	public EntityBase(Entity entity) {
		if(entity == null) {
			Log.Error("EntityBase-entity:不能为null");
		}
		this.entity = entity;
	}
	
	protected void saveOrUpdateEntity() {
		DBManager.getInstance().saveOrUpdate(entity);
	}

	public boolean hasProperty(String name) {
		return entity.hasProperty(name);
	}
	
	public void StoreProperty(String name,Object value) {
		if(IsPropertyTheSame( name, value )){
			return;
		}
		entity.setProperty(name, value);
		userEntityModified = true;
	}
	
	
	/**
	 * 添加索引属性，如果name不为索引时，将新建索引
	 * @param name
	 * @param value
	 */
	public void StoreIndexedProperty(String name,Object value) {
		if(IsPropertyTheSame( name, value )){
			return;
		}
		entity.setIndexedProperty(name, value);
		userEntityModified = true;
	}
	
	public Object getObjectProperty(String name) {
		return entity.getProperty(name);
	}
	
	public int getIntProperty(String name) {
		if(!hasProperty(name)){
			return 0;
		}
		return (int)entity.getProperty(name);
	}
	public String getStringProperty(String name) {
		return PubFunc.toString(entity.getProperty(name));
	}
	public long getLongProperty(String name) {
		if(!hasProperty(name)){
			return 0;
		}
		return (long)entity.getProperty(name);
	}
	public double getDouble(String name){
		return PubFunc.parseDouble(name);
	}
	public String getPrimaryKey() {
		return entity.getKey().getName();
	}
	
	// ------------------------------------------------------------------------
	protected <T> T GetDataObjectFromProperty(String property, Class<T> classOfT) {
		String json = getStringProperty(property);
		if (!StringUtil.NullOrEmpty(json)) {
			if (null == this.Gson) {
				this.Gson = new GsonBuilder()
						.setDateFormat(GSONUtils.DATEFORMAT)
						.disableHtmlEscaping()
						.create();
			}
			T result = this.Gson.fromJson(json, classOfT);
			return result;
		} else {
			try {
				return classOfT.newInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}
	
	// ------------------------------------------------------------------------
	protected void StoreJSONObject( Object object, String propertyName )
	{
		if( null != object )
		{
			Gson gson = new GsonBuilder()
			.setDateFormat( GSONUtils.DATEFORMAT )
			.disableHtmlEscaping()
			.create();
			String json = gson.toJson( object );
			StoreProperty( propertyName, json );
		}
	}
	
	// ------------------------------------------------------------------------
	protected boolean IsPropertyTheSame( String name, Object value )
	{
		if( true == hasProperty( name ) )
		{
			Object property = getObjectProperty( name );
			
			if( null != property && property.equals( value ) )
			{
				return true;
			}
		}
		
		return false;
	}
}
